import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def set_driver(url):

    driver_path = os.path.join(os.path.dirname(__file__), './settings/chromedriver')

    # 创建chrome参数对象
    chrome_options = Options()

    # 把chrome设置成无界面模式，不论windows还是linux都可以，自动适配对应参数
    # chrome_options.set_headless()

    chrome_options.add_argument('--no-sandbox') #解决DevToolsActivePort文件不存在的报错

    chrome_options.add_argument('window-size=1920x1080') #指定浏览器分辨率
    chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
    chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
    # chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
    # chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败

    driver = webdriver.Chrome(executable_path=driver_path,options=chrome_options)

    driver.implicitly_wait(10)

    driver.get(url)

    time.sleep(10)


    userName = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//*[@id=\"userName\"]")))

    userName.send_keys("privateTesting")

    pwd = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//*[@id=\"password\"]")))

    pwd.send_keys("huimei2020")

    button = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//*[@id=\"loginButton\"]")))

    button.click()

    login_success = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//*[@id=\"loginOut\"]/a/span"))).text

    print(login_success)

    # driver.find_element_by_xpath("//*[@id=\"userName\"]").send_keys("privateTesting")
    #
    # time.sleep(1)
    # driver.find_element_by_xpath("//*[@id=\"password\"]").send_keys("huimei2020")
    #
    # time.sleep(1)
    #
    # driver.find_element_by_xpath("//*[@id=\"loginButton\"]").click()
    #
    # time.sleep(1)
    #
    # login_success = driver.find_element_by_xpath("//*[@id=\"loginOut\"]/a/span").text
    #
    # print(login_success)



if __name__ == "__main__":

    set_driver("http://taishan.huimei.com")

